package javaeSE.package7;

import java.util.Date;
import java.util.Objects;

public class Customer {
	/*
      Object类中toString()的使用：

      1. 当我们输出一个对象的引用时，实际上就是调用当前对象的toString()
      2. Object类中toString()的定义：
        public String toString() {
            return getClass().getName() + "@" + Integer.toHexString(hashCode());
         }
      3. 像String、Date、File、包装类等都重写了Object类中的toString()方法。
         使得在调用对象的toString()时，返回"实体内容"信息
      4. 自定义类也可以重写toString()方法，当调用此方法时，返回对象的"实体内容"
     */
	public static void main(String[] args) {

		Customer cust1 = new Customer("Tom",21);
		System.out.println(cust1.toString());//com.atguigu.java1.Customer@15db9742-重写后->Customer[name = Tom,age = 21]
		System.out.println(cust1);//com.atguigu.java1.Customer@15db9742-重写后->Customer[name = Tom,age = 21]

		String str = new String("MM");
		System.out.println(str);//MM

		Date date = new Date(4534534534543L);
		System.out.println(date.toString());//Mon Sep 11 08:55:34 GMT+08:00 2113

	}



	public String name;
	public int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Customer() {
		super();
	}
	public Customer(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) return true;
		if (o == null || getClass() != o.getClass()) return false;
		Customer customer = (Customer) o;
		return age == customer.age &&
				Objects.equals(name, customer.name);
	}

	@Override
	public int hashCode() {
		return Objects.hash(name, age);
	}

	//重写的原则：比较两个对象的实体内容(即：name和age)是否相同
	//手动实现equals()的重写
//	@Override
/*	public boolean equals(Object obj) {
		if (this == obj) {
            return true;
        }
		if(obj instanceof Customer){
		//  Object obj = new Customer()   必须是同一个类下的实例才能比较
			Customer cust = (Customer)obj;
			//比较两个对象的每个属性是否都相同
			return this.age == cust.age && this.name.equals(cust.name);
		}else{
			return false;
		}
	}*/
	//手动实现  都是自动生成的不需要自己去写
//	@Override
	/*public String toString() {
		return "Customer[name = " + name + ",age = " + age + "]";
	}*/
	//自动实现
	@Override
	public String toString() {
		return "Customer [name=" + name + ", age=" + age + "]";
	}
}

class Cuss extends Customer{
	public Cuss(String name, int age) {
		super(name,age);
	}
	
	@Override
	public boolean equals(Object obj) {
		return super.equals(obj);
	}
}

